package com.abe.app;

import org.xutils.x;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.abe.clickeffect.ClickEffectTool;

public class AdaptiveFragment extends Fragment {

	protected static float scale = 0;
	private static final float UI_STANDARD_width = 1280;

	private boolean injected = false;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		injected = true;
		return x.view().inject(this, inflater, container);
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		if (!injected) {
			x.view().inject(this, this.getView());
		}
	}

	public View onCreateView(LayoutInflater inflater, int layoutResID,
			ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(layoutResID, null);
		if (!injected) {
			x.view().inject(this, view);
		}
		return this.onCreateView(inflater, view);
	}

	public View onCreateView(LayoutInflater inflater, View view) {
		if (scale == 0) {
			initScreenScale();
		}
		if (scale != 1) {
			RelayoutTool.relayoutViewHierarchy(view, scale);
			ClickEffectTool.unifiedAddClickEffect(view);
		}
		return view;
	}
	
	

	public void initScreenScale() {
		DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
		float width = displayMetrics.widthPixels;
		scale = width / UI_STANDARD_width;
	}

}
